<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: Alignment Support</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Example: Alignment Support</h1>

    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><style type="text/css" scoped>
/* Overlay Look/Feel */
.yui3-overlay-content {
    padding:2px;
    border:1px solid #000;
    background-color:#aaa;
    font-size:93%;
}

.yui3-overlay-content .yui3-widget-hd {
    font-weight:bold;
    text-align:center;
    padding:2px;
    border:2px solid #aa0000;
    background-color:#fff;
}

.yui3-overlay-content .yui3-widget-bd {
    text-align:left;
    padding:2px;
    border:2px solid #0000aa;
    background-color:#fff;
}

/* Example Layout CSS */
.overlay-example {
    position:relative;
    padding:5px;
    height:30em;
}

.overlay-example button {
    margin-left:1px;
}

.overlay-example .filler {
    color:#999;
}

.align-box {
    height:13em;
    width:13em;
    border:1px solid #000;
    margin:0px;
    text-align:center;
}

.align-box .title {
    font-weight:bold;
    color:#fff;
    padding:2px;
}

#align1 {
    position:static;
    background-color:#0000cc;
}

#align2 {
    position:relative;
    top:-200px;
    left:250px;
    background-color:#00cc00;
}

#align3 {
    position:absolute;
    bottom:130px;
    right:20px;
    background-color:#cc0000;
}

#alignment {
    padding:5px;
    display:inline;
    background-color:#fff;
    border:1px solid black;
}

#step {
    font-size:85%;
    margin-left:5px;
}
</style>

<div class="intro">
    <p>This example shows how you can use Overlay's extended positioning support to align or center the overlay either in the viewport or relative to another node on the page. You can specify any one of 9 points (top-left, top-right, bottom-left, bottom-right, top-center, bottom-center, left-center, right-center, center) to align on both the Overlay and the node/viewport it is being aligned to.</p>
</div>

<div class="example">
    <div class="overlay-example" id="overlay-align">
    <p><pre id="alignment"></pre><span id="step"></span></p>
    <p><button type="button" id="align">Next Alignment</button></p>
    <p class="filler">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nunc pretium quam eu mi varius pulvinar. Duis orci arcu, ullamcorper sit amet, luctus ut, interdum ac, quam. Pellentesque euismod. Nam tincidunt, purus in ultrices congue, urna neque posuere arcu, aliquam tristique purus sapien id nulla. Etiam rhoncus nulla at leo. Cras scelerisque nisl in nibh. Sed eget odio. Morbi elit elit, porta a, convallis sit amet, rhoncus non, felis. Mauris nulla pede, pretium eleifend, porttitor at, rutrum id, orci. Quisque non urna. Nulla aliquam rhoncus est.</p>
    <div id="align1" class="align-box"><span class="title">id = #align1</span></div>
    <div id="align2" class="align-box"><span class="title">id = #align2</span></div>
    <div id="align3" class="align-box"><span class="title">id = #align3</span></div>
</div>

<script type="text/javascript">
YUI().use("overlay", function(Y) {

    /* Create Overlay from script, this time. No footer */
    var overlay = new Y.Overlay({
        width:"10em",
        height:"10em",
        headerContent: "Aligned Overlay",
        bodyContent: "Click the 'Next Alignment' button to try a new alignment",
        zIndex:2
    });

    /* Render it to #overlay-align element */
    overlay.render("#overlay-align");

    var alignment = Y.one("#alignment");
    var stepNumber = Y.one("#step");

    /* Setup local variable for Y.WidgetPositionAlign, since we use it multiple times */
    var WidgetPositionAlign = Y.WidgetPositionAlign;

    var steps = [
        function() {
            /* Center in #overlay-align */
            overlay.set("align", {node:"#overlay-align", points:[WidgetPositionAlign.CC, WidgetPositionAlign.CC]});
            alignment.set("innerHTML", 'align: {node:"#overlay-align", points:["cc", "cc"]}');
        },
        function() {
            /* Align top-left corner of overlay, with top-right corner of #align1 */
            overlay.set("align", {node:"#align1", points:[WidgetPositionAlign.TL, WidgetPositionAlign.TR]});
            alignment.set("innerHTML", 'align: {node:"#align1", points:["tl", "tr"]}');
        },
        function() {
            /* Center overlay in #align2 */
            overlay.set("centered", "#align2");
            alignment.set("innerHTML", 'centered: "#align2"');
        },
        function() {
            /* Align right-center edge of overlay with right-center edge of viewport */
            overlay.set("align", {points:[WidgetPositionAlign.RC, WidgetPositionAlign.RC]});
            alignment.set("innerHTML", 'align: {points:["rc", "rc"]} (viewport)');
        },
        function() {
            /* Center overlay in viewport */
            overlay.set("centered", true);
            alignment.set("innerHTML", "centered: true (viewport)");
        },
        function() {
            /* Align top-center edge of overlay with bottom-center edge of #align3 */
            overlay.set("align", {node:"#align3", points:[WidgetPositionAlign.TC, WidgetPositionAlign.BC]});
            alignment.set("innerHTML", 'align: {node:"#align3", points:["tc", "bc"]}');
        }
    ];

    var step = 0;
    var totalSteps = steps.length;

    function alignNext() {
        stepNumber.set("innerHTML", "Alignment " + (step+1) + " of " + totalSteps);
        steps[step]();
        step = (++step)%(totalSteps);
    }

    alignNext();

    Y.on("click", alignNext, "#align");
});
</script>

</div>

<h2>Aligned XY Overlay Positioning - align and center Support</h2>

<h3>Setting Up The YUI Instance</h3>

<p>As with the <a href="overlay-xy.html">"Basic XY Positioning"</a> example, to create an instance of an Overlay on your page, the only module you need to request is the <code>overlay</code> module.  The <code>overlay</code> module will pull in the <code>widget</code>, <code>widget-stack</code>, <code>widget-position</code>, <code>widget-position-align</code>, <code>widget-position-constrain</code> and <code>widget-stdmod</code> extensions it uses.</p>

<pre class="code prettyprint">YUI({...}).use(&quot;overlay&quot;, function(Y) {
    &#x2F;&#x2F; We&#x27;ll write example code here, where we have a Y.Overlay class available.
});</pre>


<p>Using the <code>overlay</code> module, will also pull down the default CSS required for overlay, on top of which we only need to add our required look/feel CSS for the example.</p>

<h3>Instantiating The Overlay</h3>

<p>For this example, we'll instantiate an Overlay, as we did for the <a href="overlay-xy.html">"Basic XY Positioning"</a> example, however we'll set the content for the Overlay sections using the <code>headerContent</code> and <code>bodyContent</code> attributes. We won't create a footer section for this overlay:</p>

<pre class="code prettyprint">&#x2F;* Create Overlay from script, this time. With no footer *&#x2F;
var overlay = new Y.Overlay({
    width:&quot;10em&quot;,
    height:&quot;10em&quot;,
    headerContent: &quot;Aligned Overlay&quot;,
    bodyContent: &quot;Click the &#x27;Next Alignment&#x27; button to try a new alignment&quot;,
    zIndex:2
});

&#x2F;* Render it as a child of the #overlay-align element *&#x2F;
overlay.render(&quot;#overlay-align&quot;);</pre>


<p>Since the Overlay is created from script, and doesn't currently exist in the document, we pass the <code>overlay.render()</code> method a selector query (<code>&quot;#overlay-align&quot;</code>) for the node under which we want the Overlay to be rendered in the DOM. If we leave out this argument to render (or if the selector query doesn't bring back a node), the Overlay will be rendered to the current document's body element.</p>

<h3>Aligning the overlay</h3>

<p>The <a href="http://yuilibrary.com/yui/docs/api/WidgetPositionAlign.html"><code>WidgetPositionAlign</code></a> extension used to create the overlay class adds the <code>align</code> and <code>centered</code> attributes to the Overlay, which can be used to align or center the Overlay relative to another element on the page (or the viewport).</p>

<p>The <code>align</code> attribute accepts as a value an object literal with the following properties:</p>

<dl>
    <dt>node</dt>
    <dd>
      The node to which the Widget is to be aligned. If set to null, or not provided, the Overlay is aligned to the viewport
    </dd>
    <dt>points</dt>
    <dd>
      <p>
      A two element array, defining the two points on the Overlay and node which are to be aligned. The first element is the point on the Overlay, and the second element is the point on the node (or viewport).
      Supported alignment points are defined as static properties on the <code>WidgetPositionAlign</code> extension.
      </p>
      <p>
      e.g. <code>[Y.WidgetPositionAlign.TR, Y.WidgetPositionAlign.TL]</code> aligns the Top-Right corner of the Overlay with the
      Top-Left corner of the node/viewport, and <code>[Y.WidgetPositionAlign.CC, Y.WidgetPositionAlign.TC]</code> aligns the Center of the 
      Overlay with the Top-Center edge of the node/viewport.
      </p>
    </dd>
</dl>

<p>The <code>centered</code> property can either by set to <code>true</code> to center the Overlay in the viewport, or set to a selector string or node reference to center the Overlay in a particular node.</p>

<p>The example loops around a list of 6 alignment configurations, as the "Align Next" button is clicked:</p>

<pre class="code prettyprint">&#x2F;* Setup local variable for Y.WidgetPositionAlign, since we use it multiple times *&#x2F;
var WidgetPositionAlign = Y.WidgetPositionAlign;

...

&#x2F;* Center in #overlay-align (example box) *&#x2F;
overlay.set(&quot;align&quot;, {node:&quot;#overlay-align&quot;, 
                      points:[WidgetPositionAlign.CC, WidgetPositionAlign.CC]});

&#x2F;* Align top-left corner of overlay, with top-right corner of #align1 *&#x2F;
overlay.set(&quot;align&quot;, {node:&quot;#align1&quot;, 
                      points:[WidgetPositionAlign.TL, WidgetPositionAlign.TR]});

&#x2F;* Center overlay in #align2 *&#x2F;
overlay.set(&quot;centered&quot;, &quot;#align2&quot;);

&#x2F;* Align right-center edge of overlay with right-center edge of viewport *&#x2F;
overlay.set(&quot;align&quot;, {points:[WidgetPositionAlign.RC, WidgetPositionAlign.RC]});

&#x2F;* Center overlay in viewport *&#x2F;
overlay.set(&quot;centered&quot;, true);

&#x2F;* Align top-center edge of overlay with bottom-center edge of #align3 *&#x2F;
overlay.set(&quot;align&quot;, {node:&quot;#align3&quot;, 
                      points:[WidgetPositionAlign.TC, WidgetPositionAlign.BC]});</pre>


<h3>CSS: Overlay Look/Feel</h3>

<p>As mentioned in the <a href="overlay-xy.html">"Basic XY Positioning"</a> example, the overlay.css Sam Skin file (build/overlay/assets/skins/sam/overlay.css) provides the default functional CSS for the overlay. Namely the CSS rules to hide the overlay, and position it absolutely. However there's no default out-of-the-box look/feel applied to the Overlay widget.</p>

<p>The example provides it's own look and feel for the Overlay, by defining rules for the content box, header and body sections:</p>

<pre class="code prettyprint">.yui3-overlay-content {
    padding:2px;
    border:1px solid #000;
    background-color:#aaa;
    font-size:93%;
}

.yui3-overlay-content .yui3-widget-hd {
    font-weight:bold;
    text-align:center;
    padding:2px;
    border:2px solid #aa0000;
    background-color:#fff;
}

.yui3-overlay-content .yui3-widget-bd {
    text-align:left;
    padding:2px;
    border:2px solid #0000aa;
    background-color:#fff;
}</pre>


<h2>Complete Example Source</h2>
<pre class="code prettyprint">&lt;div class=&quot;overlay-example&quot; id=&quot;overlay-align&quot;&gt;
    &lt;p&gt;&lt;pre id=&quot;alignment&quot;&gt;&lt;&#x2F;pre&gt;&lt;span id=&quot;step&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;p&gt;
    &lt;p&gt;&lt;button type=&quot;button&quot; id=&quot;align&quot;&gt;Next Alignment&lt;&#x2F;button&gt;&lt;&#x2F;p&gt;
    &lt;p class=&quot;filler&quot;&gt;Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nunc pretium quam eu mi varius pulvinar. Duis orci arcu, ullamcorper sit amet, luctus ut, interdum ac, quam. Pellentesque euismod. Nam tincidunt, purus in ultrices congue, urna neque posuere arcu, aliquam tristique purus sapien id nulla. Etiam rhoncus nulla at leo. Cras scelerisque nisl in nibh. Sed eget odio. Morbi elit elit, porta a, convallis sit amet, rhoncus non, felis. Mauris nulla pede, pretium eleifend, porttitor at, rutrum id, orci. Quisque non urna. Nulla aliquam rhoncus est.&lt;&#x2F;p&gt;
    &lt;div id=&quot;align1&quot; class=&quot;align-box&quot;&gt;&lt;span class=&quot;title&quot;&gt;id = #align1&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
    &lt;div id=&quot;align2&quot; class=&quot;align-box&quot;&gt;&lt;span class=&quot;title&quot;&gt;id = #align2&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
    &lt;div id=&quot;align3&quot; class=&quot;align-box&quot;&gt;&lt;span class=&quot;title&quot;&gt;id = #align3&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;

&lt;script type=&quot;text&#x2F;javascript&quot;&gt;
YUI().use(&quot;overlay&quot;, function(Y) {

    &#x2F;* Create Overlay from script, this time. No footer *&#x2F;
    var overlay = new Y.Overlay({
        width:&quot;10em&quot;,
        height:&quot;10em&quot;,
        headerContent: &quot;Aligned Overlay&quot;,
        bodyContent: &quot;Click the &#x27;Next Alignment&#x27; button to try a new alignment&quot;,
        zIndex:2
    });

    &#x2F;* Render it to #overlay-align element *&#x2F;
    overlay.render(&quot;#overlay-align&quot;);

    var alignment = Y.one(&quot;#alignment&quot;);
    var stepNumber = Y.one(&quot;#step&quot;);

    &#x2F;* Setup local variable for Y.WidgetPositionAlign, since we use it multiple times *&#x2F;
    var WidgetPositionAlign = Y.WidgetPositionAlign;

    var steps = [
        function() {
            &#x2F;* Center in #overlay-align *&#x2F;
            overlay.set(&quot;align&quot;, {node:&quot;#overlay-align&quot;, points:[WidgetPositionAlign.CC, WidgetPositionAlign.CC]});
            alignment.set(&quot;innerHTML&quot;, &#x27;align: {node:&quot;#overlay-align&quot;, points:[&quot;cc&quot;, &quot;cc&quot;]}&#x27;);
        },
        function() {
            &#x2F;* Align top-left corner of overlay, with top-right corner of #align1 *&#x2F;
            overlay.set(&quot;align&quot;, {node:&quot;#align1&quot;, points:[WidgetPositionAlign.TL, WidgetPositionAlign.TR]});
            alignment.set(&quot;innerHTML&quot;, &#x27;align: {node:&quot;#align1&quot;, points:[&quot;tl&quot;, &quot;tr&quot;]}&#x27;);
        },
        function() {
            &#x2F;* Center overlay in #align2 *&#x2F;
            overlay.set(&quot;centered&quot;, &quot;#align2&quot;);
            alignment.set(&quot;innerHTML&quot;, &#x27;centered: &quot;#align2&quot;&#x27;);
        },
        function() {
            &#x2F;* Align right-center edge of overlay with right-center edge of viewport *&#x2F;
            overlay.set(&quot;align&quot;, {points:[WidgetPositionAlign.RC, WidgetPositionAlign.RC]});
            alignment.set(&quot;innerHTML&quot;, &#x27;align: {points:[&quot;rc&quot;, &quot;rc&quot;]} (viewport)&#x27;);
        },
        function() {
            &#x2F;* Center overlay in viewport *&#x2F;
            overlay.set(&quot;centered&quot;, true);
            alignment.set(&quot;innerHTML&quot;, &quot;centered: true (viewport)&quot;);
        },
        function() {
            &#x2F;* Align top-center edge of overlay with bottom-center edge of #align3 *&#x2F;
            overlay.set(&quot;align&quot;, {node:&quot;#align3&quot;, points:[WidgetPositionAlign.TC, WidgetPositionAlign.BC]});
            alignment.set(&quot;innerHTML&quot;, &#x27;align: {node:&quot;#align3&quot;, points:[&quot;tc&quot;, &quot;bc&quot;]}&#x27;);
        }
    ];

    var step = 0;
    var totalSteps = steps.length;

    function alignNext() {
        stepNumber.set(&quot;innerHTML&quot;, &quot;Alignment &quot; + (step+1) + &quot; of &quot; + totalSteps);
        steps[step]();
        step = (++step)%(totalSteps);
    }

    alignNext();

    Y.on(&quot;click&quot;, alignNext, &quot;#align&quot;);
});
&lt;&#x2F;script&gt;</pre>

</div>
        </div>

        <div id="sidebar" class="yui3-u">
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="Shows how to instantiate a basic Overlay instance, and use the Overlay&#x27;s basic XY positioning support.">
                                        <a href="overlay-xy.html">Basic XY Positioning</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Shows how to use the Overlay&#x27;s XY alignment support, to align the Overlay relative to another element, or to the viewport.">
                                        <a href="overlay-align.html">Alignment Support</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Shows how to use the Overlay&#x27;s zindex and shim support when positioning Overlays above other elements on the page.">
                                        <a href="overlay-stack.html">Stack Support</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Shows how to modify content in the Overlay&#x27;s header, body and footer sections.">
                                        <a href="overlay-stdmod.html">Standard Module Support</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Shows how to use Overlay&#x27;s constrainment support, to limit the XY value which can be set for an Overlay.">
                                        <a href="overlay-constrain.html">Constrain Support</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Shows how to create a simple plugin to retrieve content for the Overlay using the io utility.">
                                        <a href="overlay-io-plugin.html">IO Plugin</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Shows how to create a simple plugin to animate the Overlay&#x27;s movement and visibility.">
                                        <a href="overlay-anim-plugin.html">Animation Plugin</a>
                                    </li>
                                
                            
                                
                            
                                
                            
                        </ul>
                    </div>
                </div>
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples That Use This Component</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                                    <li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event&#x27;s delegation support and mouseenter event, along with the Overlay widget and Node&#x27;s support for the WAI-ARIA Roles and States.">
                                        <a href="../node-focusmanager/node-focusmanager-3.html">Accessible Menu Button</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input">
                                        <a href="../stylesheet/stylesheet-theme.html">Adjusting a Page Theme on the Fly</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
